package io.renren.modules.diycake.service.impl;

import io.renren.modules.diycake.service.CakeService;
import io.renren.modules.diycake.vo.CakeFlowVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.diycake.dao.CakeFlowDao;
import io.renren.modules.diycake.entity.CakeFlowEntity;
import io.renren.modules.diycake.service.CakeFlowService;
import org.springframework.util.ObjectUtils;


@Service("cakeFlowService")
public class CakeFlowServiceImpl extends ServiceImpl<CakeFlowDao, CakeFlowEntity> implements CakeFlowService {
@Autowired
private CakeService cakeService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<CakeFlowEntity> wrapper = new QueryWrapper<>();
        String key =  ""+params.get("key");
        if (ObjectUtils.isEmpty(key)) {
            wrapper.like("flow_name",key);
        }
        IPage<CakeFlowEntity> page = this.page(
                new Query<CakeFlowEntity>().getPage(params),
                wrapper
        );
        List<CakeFlowVo> collect = page.getRecords().stream().map(item -> {
            CakeFlowVo cakeFlowVo = new CakeFlowVo();
            BeanUtils.copyProperties(item, cakeFlowVo);
            cakeFlowVo.setCakeName(cakeService.getById(item.getCakeId()).getName());
            return cakeFlowVo;
        }).collect(Collectors.toList());
        PageUtils pageUtils = new PageUtils(page);
        pageUtils.setList(collect);
        return pageUtils;
    }

}